package com.qdsg.ysg.doctor.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseDialogFragment;
import com.qdsg.ysg.doctor.ui.adapter.AddExtraAdapter;
import com.qdsg.ysg.doctor.ui.adapter.PoliceAddressAdapter;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.qdsg.ysg.doctor.util.UtilString;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;
import com.rest.response.ConsumablesAddDto;
import com.rest.response.DocOrderBaseVO;
import com.rest.response.NurseItemsVo;
import com.rest.response.PrescriptionResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.joyoung.robot.util.LogUtil;
import jz.joyoung.robot.util.UtilList;

/**
 * Description
 * Created by lbj on 2022/4/9 17:19
 */

public class AddExtraDialog extends BaseDialogFragment {


    private Dialog mDialog;
    private OnItemClickListener mListener;
    private AddExtraAdapter addExtraAdapter;

    private static List<NurseItemsVo.ConsumablesItemVO> mList = new ArrayList<>();
    private static List<NurseItemsVo.ConsumablesItemVO> mListItems = new ArrayList<>();
    private String appointmentId;
    @SuppressLint("StaticFieldLeak")
    private static TextView tvExtraPrice;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate( R.layout.dialog_add_extra, container );

        RecyclerView rvExtra = view.findViewById( R.id.rv_extra );
        TextView tvAddExtra = view.findViewById( R.id.tv_add_extra );
        TextView tvCancel = view.findViewById( R.id.tv_cancel );
        TextView tvConfirm = view.findViewById( R.id.tv_confirm );
        tvExtraPrice = view.findViewById( R.id.tv_extra_price );
        LinearLayoutManager manager = new LinearLayoutManager( mContext );
        manager.setOrientation( LinearLayoutManager.VERTICAL );
        rvExtra.setLayoutManager( manager );
        addExtraAdapter = new AddExtraAdapter( mContext, mList );
        rvExtra.setAdapter( addExtraAdapter );


        tvAddExtra.setOnClickListener( v -> {
            mList.add( new NurseItemsVo.ConsumablesItemVO() );
            addExtraAdapter.notifyDataSetChanged();
        } );

        tvCancel.setOnClickListener( v -> {
            if (!UtilList.isEmpty( mList )) {
                mList.clear();
            }
            dismiss();
        } );
        tvConfirm.setOnClickListener( v -> {
            if (!UtilList.isEmpty( mList )) {
                addConsumables();
            }
        } );
        assert getArguments() != null;
        appointmentId = getArguments().getString( "appointmentId" );
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog( savedInstanceState );
        mDialog.setCancelable( false );
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity( Gravity.BOTTOM );
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Objects.requireNonNull( Objects.requireNonNull( getDialog() ).getWindow() ).requestFeature( Window.FEATURE_NO_TITLE );
        super.onActivityCreated( savedInstanceState );
        Objects.requireNonNull( getDialog().getWindow() ).setBackgroundDrawable( new ColorDrawable( 0x00000000 ) );
        getDialog().getWindow().setLayout( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT );
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm();
    }

    @SuppressLint("SetTextI18n")
    public static void setAllPrice() {
        double mAllPrice = 0;
        for (NurseItemsVo.ConsumablesItemVO nurseMaterialDto : mList) {
            if (UtilString.isEmpty( nurseMaterialDto.consumables )) {
                return;
            } else if (UtilString.isEmpty( nurseMaterialDto.number )) {
                return;
            }
            double mul = UtilString.mul( Double.parseDouble( nurseMaterialDto.price ), Double.parseDouble( nurseMaterialDto.number ) );
            mAllPrice = UtilString.add( mul, mAllPrice );
        }
        try {
            double div = UtilString.div( mAllPrice, 100.00, 3 );
            tvExtraPrice.setText( "¥" + div );
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }


    private void addConsumables() {
        for (NurseItemsVo.ConsumablesItemVO nurseMaterialDto : mList) {
            if (UtilString.isEmpty( nurseMaterialDto.consumables )) {
                ToastUtil.showToast( mContext, "请选择耗材" );
                return;
            } else if (UtilString.isEmpty( nurseMaterialDto.number )) {
                ToastUtil.showToast( mContext, "请选择耗材数量" );
                return;
            }
        }


        showProgressDialog( mContext );
        Map<String, Object> map = new HashMap<>();
        map.put( "id", appointmentId );
//        map.put( "totalPrice", Double.valueOf( totalPrice ).intValue() );
        map.put( "consumablesList", mList );


        RestProxy.getInstance().addConsumables( map, new Observer<ConsumablesAddDto>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ConsumablesAddDto consumablesAddDto) {
                mList.clear();
                mListener.onConfirm();
                dismiss();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( mContext, e );
                dismissProgressDialog();
            }

            @Override
            public void onComplete() {
                dismissProgressDialog();
            }
        } );
    }


    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }

    public static AddExtraDialog newInstance(String appointmentId) {
        Bundle args = new Bundle();
        args.putString( "appointmentId", appointmentId );
        AddExtraDialog fragment = new AddExtraDialog();
        fragment.setArguments( args );
        return fragment;
    }


}
